package com.rjt.cucc.controller;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.common.util.CookieUtil;
import com.rjt.common.util.DateUtil;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.RSAUtils;
import com.rjt.common.util.TextUtil;
import com.rjt.data.model.CuccInfoWeb;
import com.rjt.data.service.CuccService;

import blade.kit.http.HttpRequest;

@Controller
public class SvipController {
	

    @Autowired
    private CuccService cuccService;
    
	private  String cookie="";
	
	private String twoUrl;
	
	private String fiveUrl;
	
	private String twentyUrl;
	
	
	@PostMapping("sendRadomNum")
	@ResponseBody
	public String sendRadomNum(String mobile) {
		try {
			mobile= RSAUtils.encryptEncodeLT(mobile);
			String url="https://m.client.10010.com/mobileService/sendRadomNum.htm";
	        HttpRequest request = HttpRequest.get(url,true,"mobile",mobile,"version","android@6.002");
	        String res = request.body();
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@PostMapping("radomLogin")
	@ResponseBody
	public String radomLogin(String mobile,String password) {
		
		try {
			String enMobile= RSAUtils.encryptEncodeLT(mobile);
			String enPassword= RSAUtils.encryptEncodeLT(password);
			String url="https://m.client.10010.com/mobileService/radomLogin.htm";
			String imei=TextUtil.makeImei();
	        HttpRequest request = HttpRequest.get(url,true,
	        		"isRemberPwd","true",
	        		"loginStyle","0",
	        		"deviceId", imei,
	        		"password",enPassword,
	        		"netWay","WIFI",
	        		"mobile",enMobile,
	        		"timestamp",DateUtil.getNow(),
	        		"appId","7280aefd43e59eeaa6196073711c9e762bba491742d9e789a5f0f93c870997a8",
	        		"deviceBrand","huawei",
	        		"pip","172.16.2."+TextUtil.getRandomNum(2),
	        		"provinceChanel","general",
	        		"version","android@6.002",
	        		"deviceModel","huawei Honor V9",
	        		"deviceOS","android5.1.1",
	        		"deviceCode",imei
	        		);
	        String res = request.body();
	        this.cookie = CookieUtil.getCookie(request);
	        if(JsonUtil.getJsonValue(res, "code").equals("0")) {
	        	CuccInfoWeb web=new CuccInfoWeb();
	        	web.setMobile(mobile);
	        	web.setType("web");
	        	web.setCk(cookie);
	 			cuccService.saveP(web);
	 			ad();
	        }
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	@GetMapping("index")
	public String index() {
		return "svip";
	}
	
	@GetMapping("readyList")
	@ResponseBody
	public Object readyList(String mobile,String encryptmobile) {
		return getData(mobile,"1",encryptmobile);
	}
	@GetMapping("noReadyList")
	@ResponseBody
	public String noReadyList(String mobile,String encryptmobile) {
		return getData(mobile,"0",encryptmobile);
	}
	@GetMapping("loseList")
	@ResponseBody
	public String listNoRelay(String mobile,String encryptmobile) {
		return getData(mobile,"3",encryptmobile);
	}
	private  String getData(String mobile,String state,String encryptmobile) {
		if(encryptmobile=="") {
			return "ck已失效，请重新登录";
		}
		HttpRequest request=HttpRequest.get("https://m.client.10010.com/newuser_century/static/newUserCentury/liststate?usernumberofjsp="+encryptmobile+"&state="+state, true, 
				"mobile", mobile);
		String res = request.body();
		return res;
		
	}
	@GetMapping(value = "getLongInviCode")
	@ResponseBody
	public String getLongInviCode(String mobile) throws Exception {
		if(cookie==null || cookie==""  || mobile=="") {
			return "ck已失效，请重新登录";
		}
		String ecs_token=TextUtil.getSubString(cookie,"ecs_token=",";");
		if(ecs_token=="") {
			return "ck已失效，请重新登录";
		}
		String url="https://m.client.10010.com/mobileService/thirdRedirect.htm?redirect_uri=https://m.client.10010.com/newuser_century/static/textdl/userLogin";
        HttpRequest request = HttpRequest.post(url,true,"desmobile",mobile,"version","android@6.002","timestamp",DateUtil.getNow()).followRedirects(false).header("Cookie",this.cookie);
        String res = request.body();
        this.cookie = CookieUtil.getCookie(request);
        String redirecturl=TextUtil.getSubString(res,"<a href=\"","\"");
        request = HttpRequest.get(redirecturl).header("Cookie","ecs_token="+ecs_token);
		request.disconnect();
		res=request.body();
        return TextUtil.getSubString(res, "encryptmobile=", "&");
	}
	@GetMapping(value = "getShortInviCode")
	@ResponseBody
	public String getshortInviCode(String encryptmobile) throws Exception {
		if(encryptmobile=="") {
			return "ck已失效，请重新登录";
		}
		HttpRequest  request = HttpRequest.get("https://m.client.10010.com/newuser_century/static/newUserCentury/indexFirstPage?encryptmobile="+encryptmobile).header("Cookie",this.cookie);
	    String result = request.body();
	    request.disconnect();
	    return JsonUtil.getJsonValue(result, "basephone");
	}
	
	public  void ad() {
		try {
		    String ecs_token=TextUtil.getSubString(cookie,"ecs_token=",";");	
		    String url="https://m.client.10010.com/mobileService/thirdRedirect.htm?redirect_uri=http://m.client.10010.com/MyAccount/trafficController/myAccount.htm?cUrl=https://m.client.10010.com/mobileService/thirdRedirect.htm?redirect_uri=https://m.client.10010.com/newuser_century/static/login/userLogin";
		    HttpRequest request = HttpRequest.get(url).header("Cookie",this.cookie);
	        String res = request.body();
	        String redirecturl=TextUtil.getSubString(res,"<a href=\"","\"");
	        request = HttpRequest.get(redirecturl).header("Cookie","ecs_token="+ecs_token);
			request.disconnect();
			res=request.body();
			Document parse = Jsoup.parse(res);
			Elements elementsByTag = parse.getElementsByClass("duih huafeiduihuan hong");
			for (Element element : elementsByTag) {
				String exChangeUrl = element.attr("onclick").replace("confirm1(", "");
				exChangeUrl = exChangeUrl.replace(");", "");
				exChangeUrl = exChangeUrl.replace("'", "");
				String[] split = exChangeUrl.split(",");
				if(split.length==4) {
					if("2元话费券".equals(split[1].trim())) {
						twoUrl="http://m.client.10010.com/MyAccount/exchangeBillMark/exchange.htm?userLogin="+split[3].trim();
						twoUrl=twoUrl+"&productId="+split[0].trim()+"&productName="+split[1].trim()+"&ebCount="+split[2].trim()+"&pageFrom=1";
					}else if("5元话费券".equals(split[1].trim())){
						fiveUrl="http://m.client.10010.com/MyAccount/exchangeBillMark/exchange.htm?userLogin="+split[3].trim();
						fiveUrl=fiveUrl+"&productId="+split[0].trim()+"&productName="+split[1].trim()+"&ebCount="+split[2].trim()+"&pageFrom=1";
					}else if("20元话费券".equals(split[1].trim())){
						twentyUrl="http://m.client.10010.com/MyAccount/exchangeBillMark/exchange.htm?userLogin="+split[3].trim();
						twentyUrl=twentyUrl+"&productId="+split[0].trim()+"&productName="+split[1].trim()+"&ebCount="+split[2].trim()+"&pageFrom=1";
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@GetMapping("two")
	@ResponseBody
	public  String two() {
		try {
			if(cookie==null || cookie=="" ) {
				return "登录失效,请重新登录";
			}
			if(twoUrl==null || twoUrl=="") {
				return "每月兑换话费值上限为30元";
			}
		    HttpRequest request = HttpRequest.get(twoUrl).header("Cookie",this.cookie);
	        String res = request.body();
			request.disconnect();
			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@GetMapping("five")
	@ResponseBody
	public  String five() {
		try {
			if(cookie==null || cookie=="" ) {
				return "登录失效,请重新登录";
			}
			if(fiveUrl==null || fiveUrl=="") {
				return "每月兑换话费值上限为30元";
			}
			HttpRequest request = HttpRequest.get(fiveUrl).header("Cookie",this.cookie);
	        String res = request.body();
			request.disconnect();
			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@GetMapping("twenty")
	@ResponseBody
	public  String twenty() {
		try {
			if(cookie==null || cookie=="" ) {
				return "登录失效,请重新登录";
			}
			if(twentyUrl==null || twentyUrl=="") {
				return "每月兑换话费值上限为30元";
			}
			HttpRequest request = HttpRequest.get(twentyUrl).header("Cookie",this.cookie);
	        String res = request.body();
			request.disconnect();
			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	public static void main(String[] args) {
		SvipController svipController = new SvipController();
		svipController.cookie="c_sfbm=234g_00;path=/;jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtb2JpbGUiOiIxNzYwMDgyNjgzOCIsInBybyI6IjAxMSIsImNpdHkiOiIxMTAiLCJpZCI6ImUwZmY2ZDA1M2Q0OWEzNjZjZGRkZWExZmRlY2FlNzViIn0.Xmy4siYbmTuAGTmwOInQcYXQfPoS0pZZq6dleNJEA0k; Domain=10010.com; Path=/;ecs_token=eyJkYXRhIjoiNWVjMzc1MzNjZDhiYmJhZTEwYWQ1NDMzYjIyNDJkODc2M2Q4ZWU2M2U4ZjAxYTk5OGEzNTQ2NDcwMDFmNzI3NGY5NjQxMWU3NWRjZWVmYTlmY2M3NDczMjRlNWJlYjkwMzVjNjNhODIxNWI3ZjE1ZDU3NDgwMzhmMzdjN2RlYTI2YzNkYTVmNGU4M2M1M2MyODljZDEwM2NkYjQyNjMxNDU0ZmE4Y2JiMTI4NTM0MDUzYjdlOGYwYzQ2YzIzYzE5IiwidmVyc2lvbiI6IjAwIn0=; Domain=10010.com; Path=/;third_token=eyJkYXRhIjoiMTMyYzJlNGFmOTFiOWU0ZTRmMmMyMDQwOWVkNWU5NDJlM2JkNTk2NmUzMDdmMzYwMTY5ODg1MWIxNjMwMmI0NjY5OTRkNGYwMzE4ZWM4NGIzOTc5NGNmZjNjMWY0MjZiNDJhYzdlMGFhYjYyMTkwOWE2NzVjZTYxMWQzMDU1ZmJiY2QwNmU4M2U1YTA3NTBkNTllNzc3NmNjNWMwYTNmZiIsInZlcnNpb24iOiIwMCJ9;u_areaCode=110;wo_family=0;c_mobile=17600826838;invalid_at=1c21789190902cd9b1df7cabcea07e9b9196b8f1ed1a2356aa230e02c50852dd;t3_token=5492ed8030774c76bb6d69921a0d5dc2;cw_mutual=6ff66a046d4cb9a67af6f2af5f74c32174fc1e297e76168e140c26396069181bd3e45986776ea78b394c64501ddd5c8a7f59afa5dcb395a95818135edee49d46; Domain=.10010.com; Path=/;random_login=1;ecs_acc=XtXse0KvLuMfDSWo8GtLbQh7XCqLhjQL1bReHh45x3XTOOucpvT1zVtyYM8iDf/uvcr8TCKulwCLbQqE+F2M3BPAPC7ehBuSESp0NpLpre2LeONxih59sK9aHKyLmuslMuQySNNirnc9q66rmeq+CMa6xc3I5JGNhM0sV8/tn48=; Domain=.10010.com; Path=/;enc_acc=XtXse0KvLuMfDSWo8GtLbQh7XCqLhjQL1bReHh45x3XTOOucpvT1zVtyYM8iDf/uvcr8TCKulwCLbQqE+F2M3BPAPC7ehBuSESp0NpLpre2LeONxih59sK9aHKyLmuslMuQySNNirnc9q66rmeq+CMa6xc3I5JGNhM0sV8/tn48=;d_deviceCode=125740130416838;c_version=android@6.002;city=011|110; Domain=.10010.com; Path=/;u_account=17600826838; Domain=.10010.com; Path=/;login_type=06; Domain=.10010.com; Path=/;login_type=06;u_type=11;c_id=a56b48c1eedba70c2b44b093f6baa565364337886872cf598b366cac289d0a60;a_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTc2NjUxMzksInRva2VuIjp7ImxvZ2luVXNlciI6IjE3NjAwODI2ODM4IiwicmFuZG9tU3RyIjoieWhjYmEwZTQxNTU3MDYwMzM5In0sImlhdCI6MTU1NzA2MDMzOX0.w90hH_ZbZWCei1Ys6M1v_pq5NN0b_8argDzw7LWDLFizzd_czKleh-as5v49TKZbsAJGnLH39ZCVOPc_Ua8oVw;";
		svipController.ad();
		System.out.println(svipController.twenty());
	}
}
